package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.service.IGpdataService;
import com.stylefeng.guns.modular.system.warpper.GpdataWarpper;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-05-19 10:40:05
 */
@Controller
@RequestMapping("/gpdata")
public class GpdataController extends BaseController {

    private String PREFIX = "/system/gpdata/";

    @Autowired
    private IGpdataService gpdataService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "gpdata.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/gpdata_add")
    public String gpdataAdd() {
        return PREFIX + "gpdata_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/gpdata_update/{gpdataId}")
    public String gpdataUpdate(@PathVariable Integer gpdataId, Model model) {
        Gpdata gpdata = gpdataService.selectById(gpdataId);
        model.addAttribute("item",gpdata);
        LogObjectHolder.me().set(gpdata);
        return PREFIX + "gpdata_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Wrapper<Gpdata> wrapper = new EntityWrapper<>();
    	if(StringUtils.isNotEmpty(condition)){
    		wrapper.eq("g_dm", condition);
    	}
    	List<Map<String, Object>> gpdatas = gpdataService.selectMaps(wrapper);
    	return new GpdataWarpper(gpdatas).warp();
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Gpdata gpdata) {
        gpdataService.insert(gpdata);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer gpdataId) {
        gpdataService.deleteById(gpdataId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Gpdata gpdata) {
        gpdataService.updateById(gpdata);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{gpdataId}")
    @ResponseBody
    public Object detail(@PathVariable("gpdataId") Integer gpdataId) {
        return gpdataService.selectById(gpdataId);
    }
}
